package com.gy.yemall.yemallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gy.yemall.yemallproduct.entity.ProductAttrValueEntity;
import com.gy.yemall.yemallproduct.entity.vo.AttrAtrrGroupRelationVo;
import com.gy.yemall.yemallproduct.entity.vo.AttrVo;
import com.gy.yemall.yemallproduct.service.ProductAttrValueService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gy.yemall.yemallproduct.entity.AttrEntity;
import com.gy.yemall.yemallproduct.service.AttrService;
import com.gy.common.utils.PageUtils;
import com.gy.common.utils.R;



/**
 * 商品属性
 *
 * @author ye
 * @email 2900801226@qq.com
 * @date 2021-09-30 16:28:53
 */
@RestController
@RequestMapping("yemallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("yemallproduct:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取sku规格
     * @param spuId
     * @return
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listForSku(@PathVariable Long spuId){
        List<ProductAttrValueEntity> data = productAttrValueService.listForSku(spuId);
        return R.ok().put("data", data);
    }

    /**
     * 修改sku规格
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateForSku(@PathVariable Long spuId,
                          @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateForSku(spuId, entities);
        return R.ok();
    }

    /**
     * 显示规格参数
     * 显示销售属性
     */
    @RequestMapping("/{type}/list/{catId}")
    public R show(@RequestParam Map<String, Object> params, @PathVariable Long catId, @PathVariable String type) {
        PageUtils page = attrService.show(params, catId, type);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("yemallproduct:attr:info")
    public R info(@PathVariable("attrId") Long attrId){

		// AttrEntity attr = attrService.getById(attrId);
        AttrVo attrVo = attrService.getInfoBy(attrId);
        return R.ok().put("attr", attrVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("yemallproduct:attr:save")
    public R save(@RequestBody AttrAtrrGroupRelationVo attrAtrrGroupRelationVo){
		// attrService.save(attr);
        attrService.saveAndRelation(attrAtrrGroupRelationVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("yemallproduct:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		// attrService.updateById(attr);
        attrService.updateDetail(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("yemallproduct:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
